package com.lanyou.esb.vo;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 流量分析数据
 * 
 * @author Davey.wu
 */
public class ServiceAnalysisVo {
	/** ESB总量 **/
	private List<Entry> totals;
	/** 失败数 **/
	private List<Entry> failures;
	/** 成功数 **/
	private List<Entry> success;
	/** 排序器 **/
	private static Comparator<Entry> comparator = new Comparator<ServiceAnalysisVo.Entry>() {
		@Override
		public int compare(Entry o1, Entry o2) {
			return o1.key > o2.key ? 1 : -1;
		}
	};

	/**
	 * 排序
	 */
	public void sort() {
		if (totals != null) {
			Collections.sort(totals, comparator);
		}
		if (failures != null) {
			Collections.sort(failures, comparator);
		}
		if (success != null) {
			Collections.sort(success, comparator);
		}
	}

	public class Entry {
		private long key;
		private long value;

		public Entry(long key, long value) {
			this.key = key;
			this.value = value;
		}

		public long getKey() {
			return key;
		}

		public void setKey(long key) {
			this.key = key;
		}

		public long getValue() {
			return value;
		}

		public void setValue(long value) {
			this.value = value;
		}
	}

	public List<Entry> getTotals() {
		return totals;
	}

	public void setTotals(List<Entry> totals) {
		this.totals = totals;
	}

	public List<Entry> getFailures() {
		return failures;
	}

	public void setFailures(List<Entry> failures) {
		this.failures = failures;
	}

	public List<Entry> getSuccess() {
		return success;
	}

	public void setSuccess(List<Entry> success) {
		this.success = success;
	}

	private long getMax(List<Entry> list) {
		if (list == null || list.isEmpty()) {
			return 0l;
		}
		long max = 0l;
		for (Entry entry : list) {
			if (entry.getValue() > max) {
				max = entry.getValue();
			}
		}
		return max;
	}

	public long getMaxTotal() {
		return getMax(totals);
	}

	public long getMaxFs() {
		long maxS = getMax(success);
		long maxF = getMax(failures);
		if (maxS > maxF) {
			return maxS;
		}
		return maxF;
	}

}
